//
//  BRCouponUseListView.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/2/8.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRCouponUseListView.h"


@interface BRCouponUseListView ()<UITableViewDataSource,UITableViewDelegate>
{
    CGFloat _content_h;
    NSArray *_titles;
    UIView *_contentView;
    
    UIImageView *_defaultImg;
    UIImageView *_addImg;
    
    QMUILabel *_depositL;
    QMUILabel *_titleL;
    
}

@property (nonatomic,strong) BREmptyView *emptyLYView;
@property (nonatomic,strong) BREmptyView *netErrorView;

@end

@implementation BRCouponUseListView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //208 180
        _content_h = kScreenHeight - (kSafeAreaTopHeight + 208);
        [self setUI];
    }
    return self;
}

- (void)hidd  {
    [self endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        self.hidden =  YES;
    }];
}

- (void)show {
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeTranslation(0, -(self->_content_h));
    }];
}

- (void)requestMyCoupon:(NSString *)orderNo hasCoupon:(void(^)(BOOL has))hasCoupon {
    
    NSDictionary *dic;
    dic = @{@"orderNo":orderNo};
    NSString *url = [BRAPI orderCoupons];
    
    [self.myTableView ly_startLoading];
    [[BRRequestManager shared] GET:url params:dic succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        NSLog(@"%@",dataDict);
        NSArray *list = [BRCouponModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        if (list.count != 0) {
            BRCouponModel *model = list.firstObject;
            model.isSel = YES;
            [self toWallteBlance:model];
        }else {
            hasCoupon(NO);
        }
        self.dataList = list;
        
        [self.myTableView reloadData];
        self.myTableView.ly_emptyView = self.emptyLYView;
        [self.myTableView ly_endLoading];
//        [self.jk_viewController showBREmptyView:self.myCollection];
//        [self.jk_viewController handleEndRefreshForView:self.myCollection];
        
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
//        [self.jk_viewController showBRErrorView:self.myCollection];
//        [self.jk_viewController handleEndRefreshForView:self.myCollection];
        self.myTableView.ly_emptyView = self.netErrorView;
        [self.myTableView ly_endLoading];
        hasCoupon(NO);
    }];
    
}

- (void)toWallteBlance:(BRCouponModel *)dto {
    
    if (self.itemSelBlock) {

        self.itemSelBlock(dto,dto.isSel);
    }
}


#pragma mark - tableview delegate--
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 15;
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BRCopuonCVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRCopuonCVCell"];
    if (!cell) {
        cell = [[BRCopuonCVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BRCopuonCVCell"];
    }
    
    cell.dto = self.dataList[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (int i = 0; i < self.dataList.count; i++) {
        BRCouponModel *model = self.dataList[i];
        model.isSel = NO;
        if (i == indexPath.row) {
            model.isSel = !model.isSel;
        }
    }
    BRCouponModel *selModel = self.dataList[indexPath.row];
//    model.isSel = !model.isSel;

    [self.myTableView reloadData];
    
    [self toWallteBlance:selModel];
    
//    BRCopuonCVCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    cell.isSel = !cell.isSel;
}

#pragma mark - Set UI --
- (void)setUI  {
    //UIColorMakeWithRGBA(0, 0, 0, 0.38);
    self.backgroundColor = UIColorClear;
    
    UIView *backView = [[UIView alloc] initWithFrame:self.bounds];
    backView.backgroundColor = kColorShadow(0.2);
    [self addSubview:backView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidd)];
    [backView addGestureRecognizer:tap];
    backView.userInteractionEnabled = YES;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, _content_h)];
    contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:contentView];
    [contentView  layoutIfNeeded];
    // 设置顶部圆角
    // 绘制圆角 需设置的圆角 使用"|"来组合
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:contentView.bounds byRoundingCorners:UIRectCornerTopLeft |
    UIRectCornerTopRight cornerRadii:CGSizeMake(25, 25)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];

    // 设置大小
    maskLayer.frame = contentView.bounds;

    // 设置图形样子
    maskLayer.path = maskPath.CGPath;
    contentView.layer.mask = maskLayer;
    _contentView = contentView;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    topView.backgroundColor = UIColor.qd_backgroundColor2;
    [contentView addSubview:topView];
    
    
    QMUIButton *backBtn = [[QMUIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 52, 8, 44, 44)];
    [backBtn setBackgroundImage:UIImage.qd_cardCloseImg forState:UIControlStateNormal];
    [contentView addSubview:backBtn];
    
    [backBtn addTarget:self action:@selector(hidd) forControlEvents:UIControlEventTouchUpInside];
    
    QMUILabel *titleL = [[QMUILabel alloc] init];
    titleL.textAlignment = NSTextAlignmentLeft;
    titleL.textColor = UIColor.qd_titleTextColor;
    titleL.text = Localized(@"order_availableCoupons");
    titleL.font = [UIFont br_fontB:17];
    [contentView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backBtn).offset(0);
        make.centerX.equalTo(contentView).offset(0);
    }];
    _titleL = titleL;

    
    UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight) style:UITableViewStylePlain];

    myTableView.backgroundColor = UIColorClear;
    myTableView.rowHeight = UITableViewAutomaticDimension;
    myTableView.estimatedRowHeight = 100;
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [contentView addSubview:myTableView];
    [myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60);
        make.left.right.equalTo(contentView);
        make.bottom.equalTo(contentView);
    }];
    self.myTableView = myTableView;
    
    self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 14 + kSafeAreaBottomHeight)];
    
    self.hidden = YES;
}

- (BREmptyView *)emptyLYView {
    if (!_emptyLYView) {
        _emptyLYView = [[BREmptyView alloc] initEmptyViw];
    }
    
    return _emptyLYView;
}

- (BREmptyView *)netErrorView {
    if (!_netErrorView) {
        _netErrorView = [[BREmptyView alloc] initErrorViw];
    }
    return _netErrorView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
